﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;

namespace STT.Admin.WebForms
{
    public partial class ucTinTuc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadKhoaHoc();
        }

        void LoadKhoaHoc()
        {
            gvKhoaHoc.AutoGenerateColumns = false;
            gvKhoaHoc.DataSource = BTbKhoaHoc.SelectAll();
            gvKhoaHoc.DataBind();
        }

        protected void gvKhoaHoc_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvKhoaHoc.EditIndex = e.NewEditIndex;
            gvKhoaHoc.DataBind();
        }

        protected void gvKhoaHoc_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //BTbKhoaHoc.Update(new ETbKhoaHoc
            //{
            //    MaKhoaHoc = int.Parse(gvKhoaHoc.DataKeys[e.RowIndex].Value.ToString()),
            //    TenKhoaHoc = e.NewValues[0].ToString() != "" ? e.NewValues[0].ToString() : e.OldValues[0].ToString(),
            //    NoiDung = e.NewValues[1].ToString() != "" ? e.NewValues[1].ToString() : e.OldValues[1].ToString(),
            //    ThoiGianBatDau = DateTime.Parse(e.NewValues[2].ToString() != "" ? e.NewValues[2].ToString() : e.OldValues[2].ToString()),
            //    KinhPhi = decimal.Parse(e.NewValues[3].ToString() != "" ? e.NewValues[3].ToString() : e.OldValues[3].ToString()),
            //});

            gvKhoaHoc.EditIndex = -1;

            LoadKhoaHoc();
        }

        protected void gvKhoaHoc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int MaKhoaHoc =int.Parse(gvKhoaHoc.DataKeys[e.RowIndex].Value.ToString());

            BTbKhoaHoc.Delete(MaKhoaHoc);
            LoadKhoaHoc();
        }

        protected void gvKhoaHoc_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvKhoaHoc.EditIndex = -1;
            gvKhoaHoc.DataBind();
        }

        protected void gvKhoaHoc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    LinkButton btDelete = (LinkButton)e.Row.Cells[6].Controls[0];
                    if (btDelete != null)
                    {
                        btDelete.Attributes.Add("OnClick", "javascript:return confirm('Bạn có muốn xóa')");
                    }
                }
                catch
                {
                }                
            }
        }

        protected void btThemMoi_Click(object sender, EventArgs e)
        {

            //BTbKhoaHoc.Insert(new ETbKhoaHoc
            //{
            //     TenKhoaHoc=txtTenKhoaHoc.Text,
            //     NoiDung=txtNoiDung.Text,
            //     ThoiGian=DateTime.Parse(txtThoiGian.Text),
            //     KinhPhi=decimal.Parse(txtKinhPhi.Text),
            //});

            LoadKhoaHoc();
        }

        protected void lbThemMoi_Click(object sender, EventArgs e)
        {

        }

        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }

        protected void gvKhoaHoc_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            
        }
    }
}